package com.shengqi.xinnongren.work.ui.user.presenter;

import com.google.gson.Gson;
import com.shengqi.xinnongren.common.base.BasePresenter;
import com.shengqi.xinnongren.common.config.network.Constants;
import com.shengqi.xinnongren.common.config.network.RetrofitHelper;
import com.shengqi.xinnongren.common.config.user.UserManager;
import com.shengqi.xinnongren.common.utils.LogUtil;
import com.shengqi.xinnongren.work.model.HttpRespond;
import com.shengqi.xinnongren.work.ui.Global;
import com.shengqi.xinnongren.work.ui.user.model.RefundBean;
import com.shengqi.xinnongren.work.ui.user.view.ApplyRefundView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by LuoYaWei on 2018/8/7.
 */
public class ApplyRefundPresenter extends BasePresenter<ApplyRefundView> {
    public void getData(String orderId) {
        Map<String, Object> map = new HashMap<>();
        map.put("orderId", orderId);
        JSONObject aesJsonData = Constants.getAESJsonData(map, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), aesJsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().getRefoundData(requestBody), respond -> {
            HttpRespond<String> httpResult = Global.getHttpResult(respond);
            if (httpResult.result == 1) {
                LogUtil.e("退单数据：" + httpResult.data);
                RefundBean bean = new Gson().fromJson(httpResult.data, RefundBean.class);
                getView().showRefundRes(bean);
            } else
                getView().showRequestFailed(httpResult.message);
        });
    }

    /**
     * 勾选退钱
     *
     * @param orderId
     * @param
     */
    public void calculateMoney(String orderId, JSONArray paramsArray) {
        Map<String, Object> map = new HashMap<>();
        map.put("orderId", orderId);
        map.put("skuIdAndNum", paramsArray);
        JSONObject aesJsonData = Constants.getAESJsonData(map, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), aesJsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().calculateRefundMoney(requestBody), s -> {
            HttpRespond<String> httpResult = Global.getHttpResult(s);
            if (httpResult.result == 1) {
                LogUtil.e("退单算钱：" + httpResult.data);
                getView().showCalculateRes(httpResult.data);
            } else
                getView().showRequestFailed(httpResult.message);
        });
    }

    public void updateHeadImg(File imgFile) {
        // 创建RequestBody，传入参数："multipart/form-data"，String
        RequestBody requestToken = RequestBody.create(MediaType.parse("multipart/form-data"), UserManager.getInstance().getToken());
        RequestBody requestClient = RequestBody.create(MediaType.parse("multipart/form-data"), "android");
        // 创建RequestBody，传入参数："multipart/form-data"，File
        RequestBody requestImgFile = RequestBody.create(MediaType.parse("multipart/form-data"), imgFile);
        // 创建MultipartBody.Part，用于封装文件数据
        MultipartBody.Part requestImgPart = MultipartBody.Part.createFormData("files", imgFile.getName(), requestImgFile);
        getView().showLoadingView();
        addTask(RetrofitHelper.getInstance().getService().uploadImage(requestToken, requestClient, requestImgPart), listHttpRespond -> {
            getView().hideLoadingView();
            if (listHttpRespond.result == 1) {
                getView().onUploadPicSucceed(listHttpRespond.data);
            }
        });
    }

    public void commitRefundInfo(String orderId, JSONArray paramsArray, String reason, String pic) {
        getView().showLoadingView();
        Map<String, Object> map = new HashMap<>();
        map.put("orderId", orderId);
        map.put("skuIdAndNum", paramsArray);
        map.put("pic", pic);
        map.put("returnReason", reason);
        JSONObject aesJsonData = Constants.getAESJsonData(map, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), aesJsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().commitRefundInfo(requestBody), respond -> {
            getView().hideLoadingView();
            HttpRespond<String> httpResult = Global.getHttpResult(respond);
            if (httpResult.result == 1) {
                getView().showCommitRefundInfoRes(httpResult.message);
            } else
                getView().showRequestFailed(httpResult.message);
        });
    }
}
